﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HHPS.CreditEducation.DataProvider;
using HHPS.CreditEducation.Frameworks.Commons;
using HHPS.CreditEducation.Frameworks.Constants;

namespace HHPS.CreditEducation.Presentation.Materials
{
    public partial class SyllabusControl : UserControl
    {
        private readonly Business.Business _business;

        public SyllabusControl()
        {
            InitializeComponent();
            _business = MainView.Instance.CreditEducationBusiness;
        }

        private void SyllabusControl_Load(object sender, EventArgs e)
        {
            Dock = DockStyle.Fill;

            LoadMajor();
            LoadSubject();
            LoadCourse();

            LoadSyllabus();
        }

        private void btnAddCourse_Click(object sender, EventArgs e)
        {
            try
            {
                if (cboMajor.Items.Count < 1 || cboSubject.Items.Count < 1 || cboCourse.Items.Count < 1)
                {
                    MessageBox.Show("Bạn cần tạo đầy đủ Ngành, Môn học, Kỳ học trước khi tạo chương trình khung.");
                    return;
                }

                var syllabus = new Syllabus
                                   {
                                       IdMajor = ((Major) cboMajor.SelectedItem).Id,
                                       IdSubject = ((Subject) cboSubject.SelectedItem).Id,
                                       IdCourse = ((Course) cboCourse.SelectedItem).Id,
                                   };

                string message;
                if (_business.AddNewSyllabusValidator(syllabus, out message))
                {
                    _business.AddNewSyllabus(syllabus);

                    LoadSyllabus();
                }
                else
                    MessageBox.Show(message);
            }
            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }

        private void btnDeleteCourse_Click(object sender, EventArgs e)
        {
            try
            {
                var syllabuses = new List<Syllabus>();

                for (int i = 0; i < dgvSyllabusList.RowCount; i++)
                {
                    if (!(bool) dgvSyllabusList[(string) typeof (Syllabus).Properties().IsChecked, i].Value) continue;

                    syllabuses.Add(new Syllabus
                                       {
                                           Id = (int) dgvSyllabusList[(string) typeof (Syllabus).Properties().Id, i].Value,
                                       });
                }

                if (syllabuses.Count < 1)
                {
                    MessageBox.Show(ConstantMessages.NoSelected, FileConstants.PROGRAM_NAME);
                    return;
                }


                if (
                    MessageBox.Show(string.Format(ConstantMessages.ConfirmDeleteItem, " các khung đã chọn"),
                                    FileConstants.PROGRAM_NAME, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    _business.DeleteSyllabuses(syllabuses);
                    LoadSyllabus();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }

        private void cboMajor_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadSyllabus();
        }

        private void dgvSyllabusList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvSyllabusList == null ||
                dgvSyllabusList.Columns == null ||
                dgvSyllabusList.Columns[(string) typeof (Faculty).Properties().IsChecked] == null ||
                dgvSyllabusList.Columns[(string) typeof (Faculty).Properties().IsChecked].Index != e.ColumnIndex)
                return;

            dgvSyllabusList[e.ColumnIndex, e.RowIndex].Value =
                !(bool) dgvSyllabusList[e.ColumnIndex, e.RowIndex].Value;
        }

        #region Load Data

        /// <summary>
        /// This fuction loads facutly to datagridview
        /// </summary>
        private void LoadMajor()
        {
            cboMajor.DataSource = _business.GetListMajor();
            cboMajor.DisplayMember = (string) typeof (Major).Properties().Name;
            cboMajor.ValueMember = (string) typeof (Major).Properties().Id;
        }

        /// <summary>
        /// This fuction loads facutly to datagridview
        /// </summary>
        private void LoadSubject()
        {
            cboSubject.DataSource = _business.GetListSubject();
            cboSubject.DisplayMember = (string) typeof (Subject).Properties().Name;
            cboSubject.ValueMember = (string) typeof (Subject).Properties().Id;
        }

        /// <summary>
        /// This fuction loads facutly to datagridview
        /// </summary>
        private void LoadCourse()
        {
            cboCourse.DataSource = _business.GetListCourse();
            cboCourse.DisplayMember = (string) typeof (Course).Properties().Name;
            cboCourse.ValueMember = (string) typeof (Course).Properties().Id;
        }

        /// <summary>
        /// This fuction loads facutly to datagridview
        /// </summary>
        private void LoadSyllabus()
        {
            if (cboMajor.Items.Count < 1 || cboSubject.Items.Count < 1 || cboCourse.Items.Count < 1)
            {
                //MessageBox.Show("Bạn cần tạo đầy đủ Ngành, Môn học, Kỳ học trước khi tạo chương trình khung.");
                return;
            }

            dgvSyllabusList.DataSource = _business.GetListSyllabusByMajorId(((Major) cboMajor.SelectedItem).Id);

            DataGridViewHelper.ShowHideColumns(dgvSyllabusList, new[]
                                                                    {
                                                                        (string)
                                                                        typeof (Syllabus).Properties().IsChecked,
                                                                        (string)
                                                                        typeof (Syllabus).Properties().NumberOfCredit,
                                                                        (string)
                                                                        typeof (Syllabus).Properties().SubjectName,
                                                                        (string)
                                                                        typeof (Syllabus).Properties().CourceName,
                                                                    });
            DataGridViewHelper.ShowIsCheck(dgvSyllabusList);
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvSyllabusList, false);

            ChangeColumnsTextHeader();
            GetDependedSubject();
        }

        private void ChangeColumnsTextHeader()
        {
            dgvSyllabusList.Columns[(string) typeof (Syllabus).Properties().IsChecked].HeaderText = string.Empty;
            dgvSyllabusList.Columns[(string)typeof(Syllabus).Properties().NumberOfCredit].HeaderText = "Số tín chỉ";
            dgvSyllabusList.Columns[(string)typeof(Syllabus).Properties().SubjectName].HeaderText = "Môn học";
            dgvSyllabusList.Columns[(string)typeof(Syllabus).Properties().CourceName].HeaderText = "Kỳ học";
        }

        private void GetDependedSubject()
        {
            if (dgvSyllabusList.CurrentRow!=null)
            {
                var id = (int) dgvSyllabusList.CurrentRow.Cells[(string) typeof (Syllabus).Properties().Id].Value;
                Syllabus syllabus= _business.DataContex.Syllabuses.First(s => s.Id == id);
                if (syllabus!=null)
                {
                    dgvDependSubjectList.DataSource =
                        _business.DataContex.SubjectRequires.Where(sr => sr.IdSubject == syllabus.IdSubject);
                    DataGridViewHelper.ShowHideColumns(dgvDependSubjectList, new[]
                                                                    {
                                                                        (string)
                                                                        typeof (SubjectRequire).Properties().SubjectRequireName,
                                                                    });
                }
            }
        }

        #endregion

        private void dgvSyllabusList_CurrentCellChanged(object sender, EventArgs e)
        {
            GetDependedSubject();
        }
    }
}